package br.ucb.ucblive.HIB;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;

import br.ucb.ucblive.beans.Curso;
import br.ucb.ucblive.beans.Disciplina;
import br.ucb.ucblive.beans.Professor;
import br.ucb.ucblive.beans.Usuario;


public class DisciplinaHIB{


	@SuppressWarnings("unchecked")
	public List<Disciplina> listar() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Disciplina.class).list();
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Disciplina> listarOrdemCrescente() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Disciplina.class).
				   addOrder(Order.asc("this.nome")).
				   list();
		} finally {
			session.close();
		}
	}
	
	
	public void salvar(Disciplina obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(obj);
		transaction.commit();
		session.close();
	}
	
	
	public Disciplina consultar(int idDisciplina) {
		Session session = HibernateUtil.getSession();
		try {
			Disciplina obj = (Disciplina) session.get(Disciplina.class,idDisciplina);
			return obj;
		} finally {
			session.close();
		}
	}
	
	public void excluir(Disciplina disciplina) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(disciplina);
		transaction.commit();
		session.close();
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Curso> listarCursoByDisciplina(int idDisciplina){
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Curso.class).
				   createAlias("disciplinaCurso", "disciplina",JoinType.INNER_JOIN).
				   add(Restrictions.eq("disciplina.idDisciplina", idDisciplina)).
				   list();
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Disciplina> listarDisciplinasByProfessor(int idProfessor) {
		Session session = HibernateUtil.getSession();
		try {
				List<Disciplina> disciplinas = session.createCriteria(Disciplina.class).
			    createAlias("disciplinaProfessor", "professor", JoinType.INNER_JOIN).
			    add(Restrictions.eq("professor.idProfessor", idProfessor)).
			    list();
			return disciplinas;
		} finally {
			session.close();
		}
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List<Professor> listarProfessorByDisciplina(int idDisciplina) {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Professor.class).
				   createAlias("disciplinaProfessor", "disciplina",JoinType.INNER_JOIN).
				   add(Restrictions.eq("disciplina.idDisciplina", idDisciplina)).
				   list();
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Disciplina> listarDisciplinasByUsuario(Usuario usuarioLogin) {
		Session session = HibernateUtil.getSession();
		try {
			if(usuarioLogin.getTipo()==1){
				return session.createCriteria(Disciplina.class).
					   createAlias("disciplinaCurso", "curso").
					   createAlias("curso.moderador", "moderador").
					   createAlias("moderador.usuario", "usuario").
					   add(Restrictions.eq("usuario.login", usuarioLogin.getLogin())).
					   list();
			}else if(usuarioLogin.getTipo()==2){
				return session.createCriteria(Disciplina.class).
					   createAlias("disciplinaCurso", "curso").
					   createAlias("disciplinaProfessor", "professor").
					   createAlias("professor.usuario", "usuario").
					   add(Restrictions.eq("usuario.login", usuarioLogin.getLogin())).
					   setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).
					   list();
			}else if(usuarioLogin.getTipo()==3){
				return session.createCriteria(Disciplina.class).
					   createAlias("disciplinaCurso", "curso").
					   createAlias("curso.alunos", "aluno").
					   createAlias("aluno.usuario", "usuario").
					   add(Restrictions.eq("usuario.login", usuarioLogin.getLogin())).
					   list();
			}
			return new ArrayList<Disciplina>();
			
		} finally {
			session.close();
		}
	}
}
